/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	hey 
 * 
 * <p>描   述  :   BridgeMaintenServiceImpl.java for com.pengtu.gsj.service.impl.check.yh.bridgeTunnel    </p>
 * 
 * <p>最后修改 : $: Sep 6, 2017-3:29:45 PM v 1.0.0	 hey   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.yh.bridgeTunnel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.dl.BridgeBasicInfoDao;
import com.pengtu.gsj.dao.dl.TunnelInfoDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.dl.BridgeBasicInfo;
import com.pengtu.gsj.entity.dl.TunnelInfo;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.yh.bridgeTunnel.BridgeMaintenService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.DateUtils;
import com.pengtu.gsj.utils.MathsUtils;
import com.pengtu.gsj.utils.StringUtils;

/**
 * 
 * BridgeMaintenServiceImpl
 * 
 * Sep 6, 2017 3:29:45 PM
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class BridgeMaintenServiceImpl implements BridgeMaintenService{

	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	private BridgeBasicInfoDao bridgeBasicInfoDao;
	
	@Autowired
	private TunnelInfoDao tunnelInfoDao;
	
	@Override
	public List<BridgeBasicInfo> getBridgeBasicInfoList(QueryFilter queryFilter){
		return bridgeBasicInfoDao.find(queryFilter);
	}
	
	@Override
	public List<TunnelInfo> getTunnelInfoList(QueryFilter queryFilter){
		return tunnelInfoDao.find(queryFilter);
	}
	
	@Override
	public Map<String, Object> getBridgeMaintainingMap(String startTime, String endTime, String type, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<BridgeBasicInfo> bridgeList = new ArrayList<BridgeBasicInfo>();
		List<TunnelInfo> tunnelList = new ArrayList<TunnelInfo>();
		List<Object> values = new ArrayList<Object>();
 		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
 		
 		//责任划分明确或信息公开或档案管理到位每项得分
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		//桥梁工程师安排明确，定期培训良好每项得分
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		//资金保障良好或挂牌督办执行良好的每项得分
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		//每个公路机构桥梁工程师安排模糊或没有定期培训扣分
		double reserveMark = Double.parseDouble((String)jsonObject.get("reserveMark"));
		//分类处置良好得分
		double firstStepScore = Double.parseDouble((String)jsonObject.get("firstStepScore"));
		//最后两年每年年度报告完整得分
		double thirdStepScore = Double.parseDouble((String)jsonObject.get("thirdStepScore"));
		
		//桥梁id
		String bridgeId = (String)jsonObject.get("bridgeName");
		//隧道id
		String tunnelId = (String)jsonObject.get("tunnelName");
		
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		//获取规章制度文件
		List<Attachment> zdlist = systemManageService.getAllCheckAttachments("qlyhzd_gzzd");
		//获取年度报告 0表示国检， 1表示省检
		List<Attachment> ndList = new ArrayList<Attachment>();
		if ("0".equals(type)) {
			ndList = systemManageService.getCheckAttachmentByTag(String.valueOf(Integer.parseInt(endTime)-2), String.valueOf(Integer.parseInt(endTime)-1), "qlyhzd_yxbg");
		} else if ("1".equals(type)) {
			ndList = systemManageService.getCheckAttachmentByTag(startTime, startTime, "qlyhzd_yxbg");
		}
		
		double total =0; 				//检查得分
		double percentage =0; 			//系统完善度
		double tableFull  = yearFull*3 + yearPart*2 + poitMark*2 + firstStepScore + thirdStepScore*2;	//满分
		
		boolean flag = false;
		QueryFilter queryFilter = null;
		if (StringUtils.isEmpty(bridgeId)) {
			bridgeList = bridgeBasicInfoDao.getAll();
		} else {
			flag = true;
			values.add(bridgeId);
			queryFilter = new QueryFilter();
			queryFilter.addFilter("id", bridgeId, MatchType.EQ);
			bridgeList = this.getBridgeBasicInfoList(queryFilter);
		}
		
		if (StringUtils.isEmpty(tunnelId)) {
			tunnelList = tunnelInfoDao.getAll();
		} else {
			flag = true;
			values.add(tunnelId);
			queryFilter = new QueryFilter();
			queryFilter.addFilter("id", tunnelId, MatchType.EQ);
			tunnelList = this.getTunnelInfoList(queryFilter);
		}
		
		List<String> checkList = Arrays.asList("0","0","0","0","0","0","0","0","0");
		List<String> markList = Arrays.asList("0","0","0","0","0","0","0","0","0");
		for (Attachment attachment : zdlist) {
			
			if (attachment.getFileName().indexOf("责任划分") != -1 && "0".equals(checkList.get(0))) {
				total += yearFull;
				checkList.set(0, "1");
				markList.set(0, String.valueOf(yearFull));
			}
			if (attachment.getFileName().indexOf("公开") != -1 && "0".equals(checkList.get(1))) {
				total += yearFull;
				checkList.set(1, "1");
				markList.set(1, String.valueOf(yearFull));
			}
			if (attachment.getFileName().indexOf("档案管理") != -1 && "0".equals(checkList.get(2))) {
				total += yearFull;
				checkList.set(2, "1");
				markList.set(2, String.valueOf(yearFull));
			}
			if (attachment.getFileName().indexOf("资金保障") != -1 && "0".equals(checkList.get(3))) {
				total += poitMark;
				checkList.set(3, "1");
				markList.set(3, String.valueOf(poitMark));
			}
			if (attachment.getFileName().indexOf("挂牌督办") != -1 && "0".equals(checkList.get(4))) {
				total += poitMark;
				checkList.set(4, "1");
				markList.set(4, String.valueOf(poitMark));
			}
			if (attachment.getFileName().indexOf("定期培训") != -1 && "0".equals(checkList.get(5))) {
				total += reserveMark;
				checkList.set(5, "1");
				markList.set(5, String.valueOf(poitMark));
			}
			if (attachment.getFileName().indexOf("养护工程师") != -1 && "0".equals(checkList.get(6))) {
				total += reserveMark;
				checkList.set(6, "1");
				markList.set(6, String.valueOf(reserveMark));
			}
			if (attachment.getFileName().indexOf("分类处置") != -1 && "0".equals(checkList.get(7))) {
				total += reserveMark;
				checkList.set(7, "1");
				markList.set(7, String.valueOf(reserveMark));
			}
		}
 		//国检
		int temp =0;
		double mark =0;
		if ("0".equals(type)) {
			int years[] = {Integer.parseInt(endTime)-2,Integer.parseInt(endTime)-1};
			for (int i = 0; i < years.length; i++) {
				for (Attachment attachment : ndList) {
					if (years[i] == Integer.parseInt(DateUtils.convertDateToString("yyyy", attachment.getFileTime()))) {
						total += thirdStepScore;
						temp += 1;
						mark += thirdStepScore;
						break;
					}
				}
			}
		} else if ("1".equals(type)) {
			if (ndList.size() > 0) {
				total += thirdStepScore;
				temp += 1;
				mark += thirdStepScore;
			}
		}
		checkList.set(8, String.valueOf(temp));
		markList.set(8, String.valueOf(mark));
		
		map.put("sum",  MathsUtils.formatDouble(total,2));
		//判断完善度
		percentage = MathsUtils.divDouble(total *100 , tableFull, 2) ;
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, values);
		map.put("percent", percentage);
		map.put("flag", flag);
		map.put("bridgeList", bridgeList);
		map.put("tunnelList", tunnelList);
		map.put("att", list);
		map.put("ndList", ndList);
		map.put("zdlist", zdlist);
		map.put("checkList", checkList);
		map.put("markList", markList);
		map.put("type", type);
		map.put("endTime", endTime);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
